# # 获取和设置工作目录
# print(getwd())
# # 设置当前工作目录
# setwd("E:/R")
# print(getwd())

# # 读取csv文件
# data <- read.csv("input.csv")
# print(data)

# # 分析csv文件
# data <- read.csv("input.csv")

# print(is.data.frame(data))
# print(ncol(data))
# print(nrow(data))

# # 获得最高工资
# data <- read.csv("input.csv")
# sal <- max(data$salary)
# print(sal)

# # 获取具有最高工资的人的详细信息
# data <- read.csv("input.csv")
# # 获取最大工资
# sal <- max(data$salary)
# # 获取信息
# retval <- subset(data, salary == max(salary))
# print(retval)

# # 获取所有的IT部门员工的信息
# data <- read.csv("input.csv")
# retval <- subset(data, dept == "IT")
# print(retval)

# # 获得工资大于600的IT部门的人员
# data <- read.csv("input.csv")
# info <- subset(data, salary > 600 & dept == "IT")
# print(info)

# # 获得2014年或之后加入的人
# data <- read.csv("input.csv")
# retval <- subset(data, as.Date(start_date) > as.Date("2014-01-01"))
# print(retval)

# # 写入csv文件
# data <- read.csv("input.csv")
# retval <- subset(data, as.Date(start_date) > as.Date("2014-01-01"))
# # 写入
# # write.csv(retval, "output.csv")
# # 写入并删除附加参数
# write.csv(retval, "output.csv", row.names = FALSE)
# newData <- read.csv("output.csv")
# print(newData)


# Excel文件
# # 安装包
# install.packages("xlsx", repos="https://cran.cnr.berkeley.edu/")

# # 验证包是否安装
# any(grepl("xlsx",installed.packages()))
# # 加载包
# library("xlsx")

# # 读取Excel文件
# library("xlsx")
# data <- read.xlsx("input.xlsx", sheetIndex = 1)
# print(data)

# # # 写入二进制文件
# # 将mtcars数据写入mtcars.csv
# write.table(mtcars, file = "mtcars.csv", row.names = FALSE, na = "", col.names = TRUE, sep = ",")
# # 读取文件
# new.mtcars <- read.table("mtcars.csv", sep = ",", header = TRUE, nrows = 5)
# # 写入文件名
# write.filename = file("E:/R/data/binmtcars.dat", "wb")
# # 写为二进制文件
# writeBin(c("cyl", "am", "gear"), write.filename)
# # 写入记录
# writeBin(c(new.mtcars$cyl, new.mtcars$am, new.mtcars$gear), write.filename)
# # 关闭文件
# close(write.filename)

# # 读取二进制文件
# read.filename <- file("E:/R/data/binmtcars.dat", "rb")
# # 读取列
# column.names <- readBin(read.filename, character(), n = 3)
# # 读取文件名
# read.filename <- file("E:/R/data/binmtcars.dat", "rb")
# bindata <- readBin(read.filename, integer(), n = 18)
# # 打印
# print(bindata)
# # 读取cyl，第4-8个字节
# cyldata = bindata[4:8]
# print(cyldata)
# # 读取am，第9-13字节
# amdata = bindata[9:13]
# print(amdata)
# # 读取gear
# geardata = bindata[14:18]
# print(geardata)
# # 绑定数据
# finaldata = cbind(cyldata, amdata, geardata)
# colnames(finaldata) = column.names
# print(finaldata)

# XML文件
# # 安装包
# install.packages("XML", repos="https://cran.cnr.berkeley.edu/")

# # 读取XML文件
# library("XML")
# library("methods")
# # 读取文件
# result <- xmlParse(file = "input.xml")
# print(result)

# # 获取XML文件中存在的节点数
# library("XML")
# library("methods")
# # 读取文件
# result <- xmlParse(file = "input.xml")
# # 获取根节点
# rootnode <- xmlRoot(result)
# # 获取节点数
# rootsize <- xmlSize(rootnode)
# print(rootsize)

# # 获取第一个结点的详细信息
# library("XML")
# library("methods")
# # 读取文件
# result <- xmlParse(file = "input.xml")
# # 获取根节点
# rootnode <- xmlRoot(result)
# # 打印第一个结点详细信息
# print(rootnode[1])

# # 获取结点的不同元素
# library("XML")
# library("methods")
# # 读取文件
# result <- xmlParse(file = "input.xml")
# # 获取根节点
# rootnode <- xmlRoot(result)
# # 打印第一个结点的第一个元素
# print(rootnode[[1]][[1]])
# # 打印第一个结点的第五个元素
# print(rootnode[[1]][[5]])
# # 打印第三个几点的第二个元素
# print(rootnode[[3]][[2]])

# # XML到数据帧
# library("XML")
# library("methods")
# # 转换XML到数据帧
# xmldataframe <- xmlToDataFrame("input.xml")
# print(xmldataframe)


# JSON
# 安装包
# install.packages("rjson", repos="https://cran.cnr.berkeley.edu/")
# install.packages("C:\\Users\\mazaiting\\Desktop\\rjson_0.2.20.zip", repos = NULL, type = "source")

# # 读取JSON文件
# # 加载库
# library("rjson")
# # 读取JSON文件
# result <- fromJSON(file = "input.json")
# print(result)

# # JSON转换数据帧
# # 加载库
# library("rjson")
# # 读取JSON文件
# result <- fromJSON(file = "input.json")
# # 转换为数据帧
# json_data_frame <- as.data.frame(result)
# print(json_data_frame)

# # Web 数据
# # 安装R语言包
# install.packages("RCurl", repos="https://cran.cnr.berkeley.edu/")
# install.packages("XML", repos="https://cran.cnr.berkeley.edu/")
# install.packages("stringr", repos="https://cran.cnr.berkeley.edu/")
# install.packages("plyr", repos="https://cran.cnr.berkeley.edu/")



# # 下载文件
# library("RCurl")
# library("XML")
# library("stringr")
# library("plyr")
# # 设置url
# url <- "http://www.geos.ed.ac.uk/~weather/jcmb_ws/"
# # 获取网页链接数据
# links <- getHTMLLinks(url)
# # 获取包含JCMB_2015的文件名
# filenames <- links[str_detect(links, "JCMB_2015")]
# # 将文件名转换为列表
# filenames_list <- as.list(filenames)
# # 下载文件
# downloadcsv <- function(mainurl, filename) {
# 	filedetails <- str_c(mainurl, filename)
# 	download.file(filedetails, filename)
# }
# # 应用l_ply函数保存文件
# l_ply(filenames, downloadcsv, mainurl = "http://www.geos.ed.ac.uk/~weather/jcmb_ws/")















